/**
 * 
 */
package t4j.concurrency;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Blocker{
	synchronized void waitingCall(){
		try{
			while(!Thread.interrupted()){
				wait();//挂起线程，释放锁；唤醒时，重新获取锁
				System.out.print(Thread.currentThread() + " ");
			}
		}catch(InterruptedException e){}
	}
	
	synchronized void prod(){ notify(); }//唤醒一个等待的线程
	synchronized void prodAll(){ notifyAll(); }//唤醒所有等待的线程
}

class Task implements Runnable{
	static Blocker blocker = new Blocker();
	public void run(){ blocker.waitingCall(); }
}

class Task2 implements Runnable{
	static Blocker blocker = new Blocker();
	public void run(){ blocker.waitingCall(); }
}
public class NotifyVsNotifyAll {
	public static void main(String[] args) throws Exception {
		ExecutorService exec = Executors.newCachedThreadPool();
		
		for(int i = 0; i < 5 ; i++)
			exec.execute(new Task());
		
		exec.execute(new Task2());
		
		Timer timer = new Timer();
		
		timer.scheduleAtFixedRate(new TimerTask() {
			
			boolean prod = true;
			
			@Override
			public void run() {
				if(prod){
					System.out.print("\nnotify() ");
					Task.blocker.prod();
					prod = false;
				}else{
					System.out.print("\nnotifyAll() ");
					Task.blocker.prodAll();
					prod = true;
				}
				
			}
		}, 400, 400);//run every 0.4 second
		
		TimeUnit.SECONDS.sleep(5);
		
		timer.cancel();//计时器撤销
		
		System.out.println("\nTimer canceled");
		
		TimeUnit.MILLISECONDS.sleep(500);
		
		System.out.println("Task2.blocker.prodAll() ");
		
		Task2.blocker.prodAll();
		
		TimeUnit.MILLISECONDS.sleep(500);
		
		System.out.println("\nShutting down");
		
		exec.shutdownNow();
	}
}
